import datetime
import re

from sqlalchemy import Column, Integer, String, Boolean, TEXT, Date, DateTime, Float
from sqlalchemy.orm import declarative_base
from typing import List

Base = declarative_base()


def to_cookies(cookies: List[dict]):
    results = []
    for cookie in cookies:
        result = Cookie()
        for k in cookie.keys():
            result.__setattr__(k, cookie[k])
        results.append(result)
    return results


class Cookie(Base):
    __tablename__ = 'tb_cookie'  # 数据库表名

    name = Column(String(255), primary_key=True)
    value = Column(TEXT)
    domain = Column(String(255))
    path = Column(String(255))
    sameSite = Column('same_site', String(255))
    expiry = Column(Integer)
    httpOnly = Column('http_only', Boolean)
    secure = Column(Boolean)

    def to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self, key) is not None:
                result[key] = getattr(self, key)
        return result


class Mate(Base):
    __tablename__ = 'tb_meta1'  # 数据库表名

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255))
    state = Column(Integer)  # 状态: 0:未入库 1:就绪 2:已入库
    title = Column(TEXT)
    release_date = Column(Date)
    magnet = Column(String(255))
    captions = Column(Boolean)  # 是否字幕
    uncensored = Column(Boolean)  # 是否破解
    uncensored_origin = Column(Boolean)  # 是否破解
    leaked = Column(Boolean)  # 无码流出
    size = Column(Float)  # 无码流出
    minute = Column(Integer)  # 分钟数
    store_time = Column(DateTime, default=datetime.datetime.now())
    create_time = Column(DateTime, default=datetime.datetime.now())
    update_time = Column(DateTime)

    def release_date_format(self, some_date: str):
        self.release_date = date_format(some_date)

    def store_time_format(self, date_time: str):
        self.store_time = date_time_format(date_time)

    def suffix_format(self, file_name: str):
        self.captions = is_captions(file_name)
        self.uncensored = is_uncensored(file_name)
        self.uncensored_origin = is_uncensored_origin(file_name)
        self.leaked = is_leaked(file_name)

    def to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self, key) is not None:
                result[key] = getattr(self, key)
        return result


def date_format(some_date: str):
    if some_date is None:
        return None
    return datetime.datetime.strptime(some_date[0:10], '%Y-%m-%d').date()


def date_time_format(date_time: str):
    if date_time is None:
        return None
    return datetime.datetime.strptime(date_time[0:19], '%Y-%m-%dT%H:%M:%S')


def is_uncensored(file_name: str) -> bool:
    arr = ['-UC.', '-U.', 'uncensored', '破解']
    return len([x for x in arr if x in file_name]) > 0


def is_captions(file_name: str) -> bool:
    arr = ['-UC.', '-C.']
    return len([x for x in arr if x in file_name]) > 0


def is_leaked(file_name: str) -> bool:
    arr = ['忘记打码', '無修正', '流出']
    return len([x for x in arr if x in file_name]) > 0


def is_uncensored_origin(file_name: str) -> bool:
    hey = re.compile(r'(heyzo)[-_](\d){4}', re.I)
    common = re.compile(r'(\d){5,6}[-_](\d){2,3}', re.I)
    hey_result = hey.search(file_name)
    hey_common = common.search(file_name)
    return hey_result is not None or hey_common is not None
